package com.guli.gulimall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.guli.gulimall.order.entity.OrderEntity;
import com.guli.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.order.dao.OrderItemDao;
import com.guli.gulimall.order.entity.OrderItemEntity;
import com.guli.gulimall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 监听rabbitmq的队列
     * queues：声明需要监听的队列，支持多个
     *
     * org.springframework.amqp.core.Message
     * 参数可以写以下类型：
     * 1.Message message:原生消息详细信息  头+体
     * 2.T<发送消息的类型>  OrderReturnReasonEntity reasonEntity
     * 3.Channe channel :当前传输数据的通道
     *
     * Queue可以有很多人来监听，只要收到消息，队列就会删除消息，而且只能有一个人收到消息
     *  1）订单服务启动多个:同一个消息，只有一个客户端收到
     *  2）只有一个消息处理完，方法运行结束，我们就可以接收到下一个消息（串行）
     *
     */
    //@RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity reasonEntity, Channel channel) {
        System.out.println("接收到的消息:" + message + "-->内容：" + reasonEntity);
        //消息头属性信息
        MessageProperties properties = message.getMessageProperties();
        //消息体信息
        byte[] body = message.getBody();
//        Thread.sleep(3000);
        System.out.println("消息处理完成-->" + reasonEntity.getName());
        //通道内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag = " + deliveryTag);
        try{
            if(deliveryTag %2 ==0){
                /**
                 * 消息签收
                 * basicAck(long var1, boolean var3)
                 * var1:消息编号
                 * var3：是否批量签收。true:签收所有消息，false:单个消息签收
                 */
                channel.basicAck(deliveryTag,false);
            }else{
                /**
                 * 消息拒收
                 * basicNack(long var1, boolean var3, boolean var4)
                 *  var1：消息编号
                 *  var3：是否批量签收。true:签收所有消息，false:单个消息签收
                 *  var4: 消息丢弃还是发回服务器  true:消息重新入队  false:丢弃消息
                 *
                 * basicReject(long var1, boolean var3)
                 *  var1：消息编号
                 *  var3：是否批量签收。true:签收所有消息，false:单个消息签收
                 */
                channel.basicNack(deliveryTag,false,true);
//                channel.basicReject(deliveryTag,false);
            }
        }catch (Exception e){
            //网络中断
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMessage2(OrderEntity orderEntity) throws InterruptedException {
        System.out.println("接收到的消息:" + orderEntity);
    }

}